package com.winit.psc.repository;

import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import com.winit.psc.model.MatrixPsc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Repository
public class PscRepositoryExtImpl implements IPscRepositoryExt {

    private static final String TABLE = "matrix_psc";

    @Autowired
    private CassandraOperations cassandraOperations;

    @Override
    public List<MatrixPsc> searchByPropertiesNotNull(MatrixPsc searchConditions) {
        Select select = QueryBuilder.select().from(TABLE);
        if (null != searchConditions.getCode()) {
            select.where(QueryBuilder.eq("code", searchConditions.getCode()));
        }
        if (null != searchConditions.getId()) {
            select.where(QueryBuilder.eq("id", searchConditions.getId()));
        }
        if (null != searchConditions.getName()) {
            select.where(QueryBuilder.eq("name", searchConditions.getName()));
        }
        if (null != searchConditions.getParentId()) {
            select.where(QueryBuilder.eq("parent_id", searchConditions.getParentId()));
        }
        if (null != searchConditions.getPath()) {
            select.where(QueryBuilder.eq("path", searchConditions.getPath()));
        }
        if (null != searchConditions.getStatus()) {
            select.where(QueryBuilder.eq("status", searchConditions.getStatus()));
        }
        if (null != searchConditions.getType()) {
            select.where(QueryBuilder.eq("type", searchConditions.getType()));
        }
        select.allowFiltering();
        List<MatrixPsc> pscs = cassandraOperations.select(select, MatrixPsc.class);
        return pscs;
    }

}
